/**
 * 商家支付服务
 * 提供云豆支付相关功能
 */

const request = require('../libs/request.js')

/**
 * 获取用户云豆余额
 * @returns {Promise<Object>}
 */
const getBalance = async (params = {}) => {
    try {
        const response = await request.authGet('/v1/biz-pay/balance', {
            data: params
        })

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            throw new Error(response.msg || '获取云豆余额失败')
        }
    } catch (error) {
        console.error('获取云豆余额失败:', error)
        return {
            success: false,
            message: error.message || '获取云豆余额失败'
        }
    }
}

/**
 * 创建云豆支付
 * @param {Object} payData 支付数据 {biz_id, pay_price, pay_money}
 * @returns {Promise<Object>}
 */
const createPay = async (payData) => {
    try {
        const response = await request.authPost('/v1/biz-pay/create', payData)

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            throw new Error(response.msg || '创建支付失败')
        }
    } catch (error) {
        console.error('创建支付失败:', error)
        return {
            success: false,
            message: error.message || '创建支付失败'
        }
    }
}



/**
 * 查询支付状态
 * @param {string} payRecordId 支付记录ID
 * @returns {Promise<Object>}
 */
const queryPayStatus = async (payRecordId) => {
    try {
        const response = await request.authGet('/v1/biz-pay/status', {
            data: {
                pay_record_id: payRecordId
            }
        })

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            throw new Error(response.msg || '查询支付状态失败')
        }
    } catch (error) {
        console.error('查询支付状态失败:', error)
        return {
            success: false,
            message: error.message || '查询支付状态失败'
        }
    }
}

/**
 * 获取支付记录列表
 * @param {Object} params 查询参数 {page, limit}
 * @returns {Promise<Object>}
 */
const getPayRecords = async (params = {}) => {
    try {
        const response = await request.authGet('/v1/biz-pay/records', {
            data: {
                page: params.page || 1,
                limit: params.limit || 10,
                range:params.range || 'all',
                keyword:params.keyword || ''
            }
        })

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            throw new Error(response.msg || '获取支付记录失败')
        }
    } catch (error) {
        console.error('获取支付记录失败:', error)
        return {
            success: false,
            message: error.message || '获取支付记录失败'
        }
    }
}

/**
 * 补差价支付
 * @param {string} payRecordId 支付记录ID
 * @returns {Promise<Object>}
 */
const payRemainMoney = async (payRecordId) => {
    try {
        const response = await request.authPost('/v1/biz-pay/pay-remain', { pay_record_id: payRecordId });
        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            throw new Error(response.msg || '补差价发起失败')
        }
    } catch (error) {
        console.error('补差价发起失败:', error)
        return {
            success: false,
            message: error.message || '补差价发起失败'
        }
    }
}

const getPayRecordDetail = async (id) => {
    try {
        const response = await request.get(`/v1/biz-pay/detail`, {
            needAuth: true,
            data: {
                id
            }
        });

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg || '获取支付记录详情成功'
            };
        } else {
            return {
                success: false,
                data: null,
                message: response.msg || '获取支付记录详情失败'
            };
        }
    } catch (error) {
        return {
            success: false,
            data: null,
            message: error.message || '网络请求失败'
        };
    }
}

const startPayFromBiz = async (params = {}) => {
    try {
        const response = await request.authPost('/v1/biz-pay/start-pay-from-biz', params);
        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            throw new Error(response.msg || '从商家发起支付失败')
        }
    } catch (error) {
        console.error('从商家发起支付失败:', error)
        return {
            success: false,
            message: error.message || '从商家发起支付失败'
        }
    }
    
}

/**
 * 获取商家支付记录
 */
const recordsByBiz = async (params = {}) => {
    try {
        const response = await request.authGet('/v1/biz-pay/records-by-biz', {
            data: params
        });

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        }
    } catch (error) {
        
        return {
            success: false,
            message: error.message || '获取商家支付记录失败'
        }
    }
}

const refundByBiz = async (params = {}) => {
    try {
        const response = await request.authPost('/v1/biz-pay/refund-by-biz', params);
        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        }
    } catch (error) {
        return {
            success: false,
            message: error.message || '退款发起失败'
        }
    }
}

const doHide = async (id) => {
    try {
        const response = await request.authPost('/v1/biz-pay/do-hide', { id });
        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        }
    } catch (error) {
        return {
            success: false,
            message: error.message || '隐藏失败'
        }
    }
}

const complaint = async (orderId, content) => {
    try {
        const response = await request.post(`/v1/biz-goods-order/complaint`, {
            order_id: orderId,
            content: content,
            type:'biz-pay'
        },{
            needAuth: true,
        });

        if (response.code === 1) {
            return {
                success: true,
                message: response.msg || '投诉提交成功'
            };
        } else {
            return {
                success: false,
                message: response.msg || '投诉提交失败'
            };
        }
    } catch (error) {
        return {
            success: false,
            message: error.message || '网络请求失败'
        };
    }
}


module.exports = {
    getBalance,
    createPay,
    queryPayStatus,
    getPayRecords,
    payRemainMoney,
    getPayRecordDetail,
    startPayFromBiz,
    recordsByBiz,
    refundByBiz,
    doHide,
    complaint
} 